package com.whmnx.my.ui.viewmodel;

import android.app.Application;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Spanned;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.whmnx.base.bean.FuTextBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.utils.UItils;
import com.whmnx.my.databinding.FragmentAbout2Binding;
import com.whmnx.my.ui.fragment.AboutFragment2;

import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.utils.SPUtils;


/**
 * author : huang
 * date   : 2020/9/15 0015 14:39
 * version: 1.0
 * @author Administrator
 */
public class AboutViewModel2 extends ToolbarViewModel{
    AboutFragment2 mFragment;
    FragmentAbout2Binding mBinding;
    public AboutViewModel2(Application application) {
        super(application);
    }

    public void initToolbar(AboutFragment2 aboutFragment2, FragmentAbout2Binding binding) {
         this.mFragment = aboutFragment2;
         this.mBinding = binding;
         setTitleText("关于我们");
         initView();
    }

    private void initView() {
        Map<String, Object> params = new HashMap<>();
        int code =  mFragment.getIntent().getIntExtra("code",0);
        if (code == 0){
            code = SPUtils.getInstance().getInt("code");
        }
        if (code == 1) {
            params.put("code", "GYWM");
        }else if (code == 2){
            params.put("code", "TDGZSM");
            setTitleText("团队规则说明");
        }else if (code == 3){
            params.put("code", "SPCZSM");
            setTitleText("商品操作说明");
        }else if (code == 4){
            params.put("code", "YHXY");
            setTitleText("用户协议");
        }else if (code == 5){
            params.put("code", "YSZC");
            setTitleText("隐私政策");
        }
        HttpClient.get(mFragment, Constants.GETAGREEMENTINFO, params,new CallBack<FuTextBean>() {
            @Override
            public void onSuccess(FuTextBean fuTextBean) {
                    URLImageParser imageGetter = new URLImageParser(mBinding.tvContents);
                    //注意添加Internet权限
                    final Spanned sp = Html.fromHtml(fuTextBean.getAgreement_Content(), imageGetter, null);
                    mBinding.tvContents.setText(sp);
            }
        });
    }


    private class URLImageParser implements Html.ImageGetter {
        TextView textView;

        public URLImageParser(TextView textView) {
            this.textView = textView;
        }

        @Override
        public Drawable getDrawable(String source) {
            final URLDrawable urlDrawable = new URLDrawable();
            int w = UItils.getDeviceDensity(mFragment).widthPixels;
            RequestOptions options = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
            Glide.with(mFragment)
                    .asBitmap()
                    .load(source)
                    .apply(options)
                    .into(new SimpleTarget<Bitmap>(w,(w/2)) {
                        @Override
                        public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                            urlDrawable.bitmap = resource;
                            urlDrawable.setBounds(0, 0, resource.getWidth(), resource.getHeight());

                            textView.invalidate();
                            textView.setText(textView.getText());
                        }
                    });
            return urlDrawable;
        }
    }

    private class URLDrawable extends BitmapDrawable {
        protected Bitmap bitmap;

        @Override
        public void draw(Canvas canvas) {
            if(bitmap != null) {
                canvas.drawBitmap(bitmap, 0, 0, getPaint());
            }
        }
    }
}
